# -*- coding: utf-8 -*-

from airspeed import CachingFileLoader
from core.settings import WEB_ROOT
from base.structs import ComponentMgr,FieldType
from base.reader import SelectorReader, TypesReader, MenuReader, SpecialReader
import os

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class Generator :
    
    def __init__(self,manager):
        self.loader = CachingFileLoader(WEB_ROOT + "/meta/")
        self.manager = manager
    
    def produce(self,meta_file,context,target_file = None):
        template = self.loader.load_template(meta_file)
        result = template.merge(context, self.loader)
        if target_file :
            real_path = WEB_ROOT + '/' + target_file
            self.__ensureFile(real_path)
            handler = open(real_path,"w")
            handler.writelines(str(result).encode('utf-8'))
            handler.close()
        return result
    
    def __ensureFile(self,file_path):
        if os.path.isfile(file_path) :
            return
        self.__ensureDir(os.path.split(file_path)[0])
 
    def __ensureDir(self,dir_path):
        if os.path.isdir(dir_path) :
            return;
        head,tail = os.path.split(dir_path)
        if head :
            self.__ensureDir(head)
        if tail :
            os.mkdir(dir_path)
        
    def loadConfig(self):
        SelectorReader.read('selects.xml', self.manager)
        TypesReader.read('types.xml', self.manager)
        MenuReader.read('menus.xml', self.manager)
        SpecialReader.read('specials.xml', self.manager)
        
    def doGodWorks(self):
        print "create Index Page"
        self.__createIndexPage()
        print "create Panel Page"
        self.__createPanelPage()
        print "create List Page"
        self.__createListPage()
        print "create Add Page"
        self.__createAddPage()
        print "create Edit Page"
        self.__createEditPage()
        print "create Show Page"
        self.__createShowPage()
        print "create Export Page"
        self.__createExportPage()
        print "create Customer Page"
        self.__createCustomerPage()
        print "create Urls"
        self.__createUrls()
        print "create Models"
        self.__createModels()
        print "create Trans"
        self.__createTrans()
        print "create Views"
        self.__createViews()
        print "create Specials"
        self.__createSpecials()
        print "create Deploys"
        self.__createDeploys()
        print "create Extractors"
        self.__createExtractors()
        print "All code generated!"
    
    def __createIndexPage(self):
        self.produce('html/index.tmpl', {'menus':self.manager.menus.values(),'FieldType':FieldType},'index.html')
        
    def __createPanelPage(self):
        for menu in self.manager.menus.values() :
            for element in menu.elements.values() :
                target_file = 'pages/' + element.key + '/' + element.key + '.html'
                self.produce('html/dic_panel.tmpl', {'element':element,'FieldType':FieldType},target_file)

    def __createListPage(self):
        for menu in self.manager.menus.values() :
            for element in menu.elements.values() :
                target_file = 'templates/' + element.key + '/list.tpl'
                self.produce('tpl/dic_list.tmpl', {'element':element,'FieldType':FieldType},target_file)

    def __createAddPage(self):
        for menu in self.manager.menus.values() :
            for element in menu.elements.values() :
                target_file = 'pages/' + element.key + '/add.html'
                self.produce('html/dic_add.tmpl', {'element':element,'FieldType':FieldType},target_file)
    
    def __createEditPage(self):
        for menu in self.manager.menus.values() :
            for element in menu.elements.values() :
                target_file = 'pages/' + element.key + '/edit.html'
                self.produce('html/dic_edit.tmpl', {'element':element,'FieldType':FieldType},target_file)

    def __createShowPage(self):
        for menu in self.manager.menus.values() :
            for element in menu.elements.values() :
                target_file = 'pages/' + element.key + '/show.html'
                self.produce('html/dic_show.tmpl', {'element':element,'FieldType':FieldType},target_file)

    def __createExportPage(self):
        for menu in self.manager.menus.values() :
            for element in menu.elements.values() :
                target_file = 'pages/' + element.key + '/export.html'
                self.produce('html/dic_export.tmpl', {'element':element,'FieldType':FieldType},target_file)

    def __createCustomerPage(self):
        for customer in self.manager.customers.values() :
            target_file = 'pages/customer/' + customer.name + '.html'
            self.produce('html/customer.tmpl', {'customer':customer,'specials':self.manager.specials.values(),'FieldType':FieldType},target_file)
    
    def __createUrls(self):
        target_file = 'src/mvc/urls.py'
        self.produce('py/urls.tmpl', {'menus':self.manager.menus.values(),'specials':self.manager.specials.values(),'FieldType':FieldType},target_file)
    
    def __createModels(self):
        target_file = 'src/mvc/models.py'
        self.produce('py/models.tmpl', {'menus':self.manager.menus.values(),'FieldType':FieldType},target_file)
    
    def __createTrans(self):
        target_file = 'src/mvc/trans.py'
        self.produce('py/trans.tmpl', {'menus':self.manager.menus.values(),'FieldType':FieldType},target_file)
    
    def __createViews(self):
        target_file = 'src/mvc/views.py'
        self.produce('py/views.tmpl', {'menus':self.manager.menus.values(),'FieldType':FieldType},target_file)

    def __createSpecials(self):
        target_file = 'src/mvc/specials.py'
        self.produce('py/specials.tmpl', {'specials':self.manager.specials.values(),'FieldType':FieldType},target_file)

    def __createDeploys(self):
        target_file = 'src/tools/deploys.py'
        self.produce('py/deploys.tmpl', {'menus':self.manager.menus.values(),'FieldType':FieldType},target_file)
        
    def __createExtractors(self):
        target_file = 'src/tools/extractors.py'
        self.produce('py/extractors.tmpl', {'menus':self.manager.menus.values(),'FieldType':FieldType},target_file)

engine = Generator(ComponentMgr())

def gencode():
    engine.loadConfig()
    engine.doGodWorks()

if __name__ == "__main__":
    gencode()